package com.xiaoyu.izf.aspect;

import com.xiaoyu.izf.exception.CustomException;
import com.xiaoyu.izf.exception.JsonReturnedException;
import com.xiaoyu.izf.model.exception.BusinessStatusEnum;
import com.xiaoyu.izf.model.page.FormResultBean;
import com.xiaoyu.izf.model.page.Status;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionAspect {

    /**
     * 未登录异常
     * @param e
     * @return
     */
    @ExceptionHandler(value={CustomException.class})
    public String exceptionHandler(CustomException e){
        int code = e.getCode();
        if (BusinessStatusEnum.NOT_LOGIN.status()==code){
            return "redirect:/";
        }
        /*
        else if (BusinessStatusEnum.NO_PERMISSION.status()==code){
            return "redirect:/no_permission_page";
        }
         */
        return "";
    }

    @ExceptionHandler(value={JsonReturnedException.class})
    @ResponseBody
    public FormResultBean exceptionHandler(JsonReturnedException e){
        return new FormResultBean(0,e.getType(),e.getMessage());
    }
}
